//
//  QNBlank.m
//  QNReuseTool
//
//  Created by rsh on 2019/10/15.
//  Copyright © 2019年 rsh. All rights reserved.
//

#import "QNBlank.h"

@interface QNBlank ()

@property (strong, nonatomic) UIView *maskView;
@property (strong, nonatomic) UIImageView *icon;
@property (strong, nonatomic) UILabel *title;
@property (strong, nonatomic) UILabel *message;
@property (copy  , nonatomic) void (^callBack)(void);

@end

@implementation QNBlank

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.maskView = UIView.alloc.init;
        self.maskView.backgroundColor = UIColor.whiteColor;
        [self addSubview:self.maskView];
        
        [self.maskView setTranslatesAutoresizingMaskIntoConstraints:NO];
        NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.maskView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
        NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:self.maskView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
        NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:self.maskView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
        NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:self.maskView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
        [self addConstraints:@[top, left, bottom, right]];
        
        self.icon = UIImageView.alloc.init;
        self.icon.contentMode = UIViewContentModeCenter;
        self.icon.backgroundColor = UIColor.whiteColor;
        [self.maskView addSubview:self.icon];
        
        [self.icon setTranslatesAutoresizingMaskIntoConstraints:NO];
        NSLayoutConstraint *icx = [NSLayoutConstraint constraintWithItem:self.icon attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.maskView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
        NSLayoutConstraint *icy = [NSLayoutConstraint constraintWithItem:self.icon attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.maskView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
        [self addConstraints:@[icx, icy]];
        
        self.title = UILabel.alloc.init;
        self.title.textColor = UIColor.blackColor;
        self.title.font = [UIFont systemFontOfSize:16];
        self.title.textAlignment = NSTextAlignmentCenter;
        self.title.text = @"页面提示";
        [self.maskView addSubview:self.title];
        
        [self.title setTranslatesAutoresizingMaskIntoConstraints:NO];
        NSLayoutConstraint *ttop = [NSLayoutConstraint constraintWithItem:self.title attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.icon attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20];
        NSLayoutConstraint *tleft = [NSLayoutConstraint constraintWithItem:self.title attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.maskView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
        NSLayoutConstraint *tright = [NSLayoutConstraint constraintWithItem:self.title attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.maskView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
        
        [self.maskView addConstraints:@[ttop, tleft, tright]];
        
        self.message = UILabel.alloc.init;
        self.message.textColor = [UIColor.blackColor colorWithAlphaComponent:0.5];
        self.message.font = [UIFont systemFontOfSize:14];
        self.message.textAlignment = NSTextAlignmentCenter;
        self.message.numberOfLines = 2;
        self.message.text = @"暂无数据，点击空白处可刷新";
        [self.maskView addSubview:self.message];
        
        [self.message setTranslatesAutoresizingMaskIntoConstraints:NO];
        NSLayoutConstraint *mtop = [NSLayoutConstraint constraintWithItem:self.message attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.title attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10];
        NSLayoutConstraint *mleft = [NSLayoutConstraint constraintWithItem:self.message attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.maskView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
        NSLayoutConstraint *mright = [NSLayoutConstraint constraintWithItem:self.message attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.maskView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
        
        [self.maskView addConstraints:@[mtop, mleft, mright]];
        
        UITapGestureRecognizer *tap = [UITapGestureRecognizer.alloc initWithTarget:self action:@selector(viewTapCallBack)];
        [self.maskView addGestureRecognizer:tap];
    }
    return self;
}

- (void)viewTapCallBack {
    if (self.callBack) {
        self.callBack();
    }
}

+ (instancetype)blankInView:(UIView *)view withImage:(UIImage *)img title:(NSString *)title message:(NSString *)message touchCallBack:(void (^)(void))callBack{
    
    QNBlank *blank = nil;
    for (UIView *sub in view.subviews) {
        if ([sub isKindOfClass:QNBlank.class]) {
            blank = (QNBlank *)sub;
            break;
        }
    }
    
    blank = [QNBlank.alloc initWithFrame:view.bounds];
    blank.callBack = callBack;
    blank.icon.image = img;
//    [UIImage imageNamed:name];
    if (title)   blank.title.text = title;
    if (message) blank.message.text = message;
    [view addSubview:blank];
    
    return blank;
    
}

//+ (instancetype)blankInView:(UIView *)view withImageName:(NSString *)name title:(NSString *)title message:(NSString *)message touchCallBack:(void (^)(void))callBack {
//
//    ASBlank *blank = nil;
//    for (UIView *sub in view.subviews) {
//        if ([sub isKindOfClass:ASBlank.class]) {
//            blank = (ASBlank *)sub;
//            break;
//        }
//    }
//
//    blank = [ASBlank.alloc initWithFrame:view.bounds];
//    blank.callBack = callBack;
//    blank.icon.image = [UIImage imageNamed:name];
//    if (title)   blank.title.text = title;
//    if (message) blank.message.text = message;
//    [view addSubview:blank];
//
//    return blank;
//}

+ (void)blankDismissFromView:(UIView *)view {
    [view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:self.class]) {
            [UIView animateWithDuration:0.25 animations:^{
                obj.alpha = 0;
            } completion:^(BOOL finished) {
                [obj removeFromSuperview];
            }];
        };
    }];
}

- (void)dismiss {
    [UIView animateWithDuration:0.1 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
